package com.songlanyun.modules.rabbitmq.interactmsg.impl;

import cn.hutool.core.util.ObjectUtil;
import com.songlanyun.common.enums.PrivateMessageConstant;
import com.songlanyun.common.enums.SysConstant;
import com.songlanyun.common.utils.push.GeTuiUtils;
import com.songlanyun.common.utils.push.Msg;
import com.songlanyun.modules.account.entity.UserInfoEntity;
import com.songlanyun.modules.account.service.UserInfoService;
import com.songlanyun.modules.applyareaagent.entity.ApplyAreaAgentEntity;
import com.songlanyun.modules.applyareaagent.service.ApplyAreaAgentService;
import com.songlanyun.modules.message.entity.PrivateMessage;
import com.songlanyun.modules.message.model.InteractionMessage;
import com.songlanyun.modules.message.model.SystemMessage;
import com.songlanyun.modules.message.model.to.MessageTO;
import com.songlanyun.modules.message.model.to.PrivateMessageTO;
import com.songlanyun.modules.message.service.PrivateMessageService;
import com.songlanyun.modules.rabbitmq.interactmsg.IInteractMsgService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 区县运营商审核
 */
@Service("areaAgentCheckInteractService")
public class AreaAgentCheckInteractServiceImpl implements IInteractMsgService {

    @Resource
    private PrivateMessageService privateMessageService;

    @Resource
    private GeTuiUtils geTuiUtils;

    @Autowired
    private ApplyAreaAgentService applyAreaAgentService;

    @Override
    public void savePrivateMsg(MessageTO to) {
        //查询审核消息
        ApplyAreaAgentEntity entity = applyAreaAgentService.loadById(to.getModelId(), false);
        if (ObjectUtil.isNull(entity) || ObjectUtil.equal(entity.getStatus(), 0)) {
            return;
        }

        String remark;
        //审核通过
        if (ObjectUtil.equal(entity.getStatus(), 1)) {
            remark = "你的审核已通过，请联系平台客服推进后续事宜";
        } else if (ObjectUtil.equal(entity.getStatus(), 2)) {
            remark = "你的审核未通过" + (StringUtils.isNotBlank(entity.getCheckInfo()) ? "，" + entity.getCheckInfo() : "");
        } else {
            return;
        }

        //保存消息
        SystemMessage systemMessage = new SystemMessage(SysConstant.SystemMessageType.AREA_AGENT_CHECK, to.getModelId());
        PrivateMessageTO privateMessageTO = new PrivateMessageTO(to.getUserId(), PrivateMessageConstant.MessageType.SYSTEM_MESSAGES, systemMessage, remark);

        PrivateMessage privateMessage = new PrivateMessage(privateMessageTO);
        privateMessageService.save(privateMessage);

        //单推
        geTuiUtils.pushByUserId(
                Msg.builder()
                        .uid(to.getUserId())
                        .title(PrivateMessageConstant.MessageType.SYSTEM_MESSAGES.getMsg())
                        .content(remark)
                        .msg(privateMessage.pushMessageBody())
                        .build()
        );
    }
}
